<template>
    <div class="views-lizhishenqing-add">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加离职申请 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item v-if="isRead" label="工号" prop="gonghao"> {{ form.gonghao }} </el-form-item>

                    <el-form-item v-if="isRead" label="性别" prop="xingbie"> {{ form.xingbie }} </el-form-item>

                    <el-form-item v-if="isRead" label="护工姓名" prop="hugongxingming"> {{ form.hugongxingming }} </el-form-item>

                    <el-form-item v-if="isRead" label="护工电话" prop="hugongdianhua"> {{ form.hugongdianhua }} </el-form-item>

                    <el-form-item label="申请编号" prop="shenqingbianhao" :rules="[{required:true, message:'请填写申请编号'}]">
                        <el-input type="text" placeholder="输入申请编号" style="width: 450px" v-model="form.shenqingbianhao" />
                    </el-form-item>

                    <el-form-item label="离职原因" prop="lizhiyuanyin" required :rules="[{required:true, message:'请填写离职原因'}]">
                        <el-input type="textarea" v-model="form.lizhiyuanyin"></el-input>
                    </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useLizhishenqingCreateForm, canLizhishenqingInsert } from "@/module";

    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const { form, readMap } = useLizhishenqingCreateForm(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        return new Promise((resolve, reject) => {
            formModel.value
                .validate()
                .then((res) => {
                    if (loading.value) return;
                    loading.value = true;
                    canLizhishenqingInsert(form).then(
                        (res) => {
                            loading.value = false;
                            if (res.code == 0) {
                                emit("success", res.data);
                                if (props.isHouxu) {
                                    ElMessage.success("添加成功");
                                    router.go(-1);
                                }
                            } else {
                                ElMessageBox.alert(res.msg);
                            }
                            resolve(res);
                        },
                        (err) => {
                            loading.value = false;
                            ElMessageBox.alert(err.message);
                            reject(err);
                        }
                    );
                })
                .catch((err) => {
                    reject(err);
                });
        });
    };
</script>

<style scoped lang="scss">
    .views-lizhishenqing-add {
    }
</style>
